﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace CodeTop.复习九月份
{
    public class Solution43
    {
        public string Multiply(string num1, string num2)
        {
            //确保nums1是长的
            IList<int> list = new List<int>();

            string res = "";
            int len1 = num1.Length;
            int len2 = num2.Length;

            if (len1 < len2)
            {
                return Multiply(num2, num1);
            }
            else
            {
                for (int i = len2 - 1; i >= 0; i--)
                {
                    //计算当前位和nums1的积
                    string cur = Mul(num1, num2[i]);
                    Console.WriteLine("Mulcur:   " + cur + "   Mulres   " + res);
                    for (int j = 0; j < len2 - i - 1; j++)
                    {
                        cur += "0";
                    }
                    //字符串相加
                    res = AddStr(res, cur);//cur要左移k位
                    Console.WriteLine(res);
                }
            }

            Console.WriteLine(res);

            return res.ToString();
        }

        private string AddStr(string res, string cur)
        {
            Stack<string> stack = new Stack<string>();
            Console.WriteLine("res:   " + res + "   cur   " + cur);

            int lenC = cur.Length;
            int lenR = res.Length;
            if (lenC < lenR)
            {
                return AddStr(cur, res);//c长
            }
            int next = 0;//进位
            for (int i = 0; i < lenR; i++)
            {
                int curNumC = cur[lenC - i - 1] - '0';
                int curNumR = res[lenR - i - 1] - '0';

                int num = curNumC + curNumR + next;
                next = num / 10;
                int curNum = num % 10;

                Console.WriteLine("curNumC:   " + curNumC + "   curNumR   " + curNumR + "   curNum   " + curNum);

                stack.Push(curNum.ToString());
            }
            for (int i = lenR; i < lenC; i++)
            {
                int curNumC = cur[lenC - i - 1] - '0';

                int num = curNumC + next;
                next = num / 10;
                int curNum = num % 10;

                stack.Push(curNum.ToString());
            }
            if (next != 0)
            {
                stack.Push(next.ToString());
            }

            string resStr = "";
            while (stack.Count > 0)
            {
                resStr += stack.Pop();
            }
            Console.WriteLine("相加的结果Add：   " + resStr);
            return resStr;
        }

        private string Mul(string num, char v)
        {
            Console.WriteLine("num:   " + num + "   v   " + v);
            int numv = v - '0';

            int next = 0;
            Stack<string> stack = new Stack<string>();

            for (int i = num.Length - 1; i >= 0; i--)
            {
                int curnum = num[i] - '0';

                int nums = curnum * numv + next;
                next = nums / 10;
                int cur = nums % 10;

                // pre =cur* (int)Math.Pow(10, num.Length-i-1) + pre;

                stack.Push(cur.ToString());
            }
            //pre = next * (int)Math.Pow(10, num.Length) + pre;
            if (next != 0)
            {
                stack.Push(next.ToString());
            }

            string res = "";
            while (stack.Count > 1 && stack.Peek() == "0")
            {
                stack.Pop();
            }
            while (stack.Count > 0)
            {
                res += stack.Pop();
            }
            Console.WriteLine("相乘的结果Mul：   " + res);

            return res;
        }
    }

    internal class Program43
    {
        public static void Main43(string[] args)
        {
            Solution43 solution = new Solution43();
            string num1 = "9133", num2 = "0";
            solution.Multiply(num1, num2);
        }
    }
}